'use strict';

const Utility = require('../libs/utility');



module.exports = function () {

    var model = this.url.pathname.split('/');    model.shift();

    const index = -((model.length % 2)  ||  2);

    const equal = { },  _equal_ = model.slice(0, index);

    for (var i = 0;  _equal_[i];  i++)
        equal[_equal_[i]] = _equal_[i++];

    model = model.slice( index );

    if ( model[1] )  equal.id = model[1];

    model = require( Utility.fromCWD(`data/${model[0]}`) );

    model = new model(this, Object.assign(
        this.trig('model', this.request.params),  equal
    ));

    switch ( this.request.method ) {
        case 'GET':
            return  model.search( this.request.query );
        case 'POST':
            return  model.append();
        case 'PUT':
            return  model.update();
        case 'DELETE':
            return  model.remove();
    }
};
